﻿using System;
using System.Collections.Generic;
using System.Data;

namespace NTL.DAL
{
	public abstract class GalleriesProvider : DataAccess
	{
		static private GalleriesProvider _instance = null;
		static public GalleriesProvider Instance
		{
			get
			{
				if (_instance == null)
					_instance = (GalleriesProvider)Activator.CreateInstance(
					   Type.GetType("NTL.DAL.SqlClient.SqlGalleriesProvider"));
				return _instance;
			}
		}

		public GalleriesProvider()
		{
			this.ConnectionString = Globals.Settings.ConnectionString;
			this.EnableCaching = Globals.Settings.EnableCaching;
			this.CacheDuration = Globals.Settings.CacheDuration;
		}

		public abstract GalleryDetails FindById(long id);
		public abstract List<GalleryDetails> FindByTimeline(int year, long pageIndex, long pageSize);
		public abstract List<GalleryDetails> FindByTimeline(int year, int month, long pageIndex, long pageSize);
		public abstract List<GalleryDetails> FindByTimeline(int year, int month, int day, long pageIndex, long pageSize);
		public abstract long CountByTimeline(int year);
		public abstract long CountByTimeline(int year, int month);
		public abstract long CountByTimeline(int year, int month, int day);

		public abstract long Insert(GalleryDetails gallery);
		public abstract bool Update(GalleryDetails gallery);

		protected virtual GalleryDetails GetFromReader(IDataReader reader)
		{
			return new GalleryDetails(
				(long)reader["GalleryId"],
				reader["Name"].ToString(),
				(DateTime)reader["AddedDate"],
				(DateTime)reader["Timeline"],
				reader["AddedBy"].ToString(),
				(bool)reader["Visible"],
				(bool)reader["CommentEnabled"]);
		}
		protected virtual List<GalleryDetails> GetCollectionFromReader(IDataReader reader)
		{
			List<GalleryDetails> galleries = new List<GalleryDetails>();
			while (reader.Read())
				galleries.Add(GetFromReader(reader));
			return galleries;
		}
	}
}
